$(function(){
	$(".j-wscroll-row").each(function(index, element) {
		var linum = $(this).find('li').length;
		var liHeight = $(this).children().height();
		var liWidth = $(this).children().width();

		$(this).width(liWidth*linum);
        var parentwidth =  $(this).parent().css({'overflow':'hidden'}).width();
		$(this).wrap("<div class='parentscrollcon'></div>").parent().width(parentwidth).css({'overflow':'hidden','position':'relative','height':liHeight})
		$(this).parent().myScroll({
			speed:20, //数值越大，速度越慢
			rowWidth:304 //li的宽度
		});
    });
});
// JavaScript Document
(function($){
	$.fn.myScroll = function(options){
	//默认配置
	var defaults = {
		speed:50,  //滚动速度,值越大速度越慢
		rowWidth:24 //每行的高度
	};
	
	var opts = $.extend({}, defaults, options),intId = [];
	
	function marquee(obj, step){
		
		obj.find(".j-wscroll-row").css({'position':'absolute','top':0}).animate({
			left: '-=1'
		},0,function(){
				var s = -parseInt($(this).css("left"));
				if(s >= step){
					$(this).children().slice(0, 1).appendTo($(this));
					$(this).css("left", 0);
				}
			});
		}
	/*function marquee_r(obj, step){
		obj.find(".j-wscroll-row").css({'position':'absolute','top':0}).animate({
			right: '-=1'
		},0,function(){
				var s = Math.abs(parseInt($(this).css("right")));
				linum = $(this).children().length;
				
				if(s % step == 0){
					$(this).children().slice(linum-1,linum).insertBefore($(this).children().slice(0, 1));
					$(this).css("right",0);
				}
			});
		}*/
		this.each(function(i){
			var sh = opts["rowWidth"],speed = opts["speed"],_this = $(this);
			intId[i] = setInterval(function(){
				if(_this.find(".j-wscroll-row").width()<=_this.width()){
					clearInterval(intId[i]);
				}else{
					marquee(_this, sh);
				}
			}, speed);

			_this.hover(function(){
				clearInterval(intId[i]);
			},function(){
				intId[i] = setInterval(function(){
					if(_this.find(".j-wscroll-row").width()<=_this.width()){
						clearInterval(intId[i]);
					}else{
						marquee(_this, sh);
					}
				}, speed);
			});
		
		});
		
		$('.prev-btn').click(function(){
				var rowbar = $(this).parent().find('.j-wscroll-row');
				var linum = rowbar.children().length;
				var curleft = parseInt(rowbar.css('left'));
				var liwidth = rowbar.children().width();
				var goleft = curleft-liwidth;
				if(curleft-liwidth>liwidth){goleft=liwidth}
				rowbar.children().slice(linum-1,linum).insertBefore(rowbar.children().slice(0, 1));
				rowbar.css({'left':goleft})
				rowbar.stop().animate({'left':0},300);
				
			})/*prev-btn*/
		$('.next-btn').click(function(){
			
				var rowbar = $(this).parent().find('.j-wscroll-row');
				var linum = rowbar.children().length;
				var curleft = parseInt(rowbar.css('left'));
				var liwidth = rowbar.children().width();
				var goleft = curleft+liwidth;
				rowbar.children().slice(0, 1).appendTo(rowbar);
				rowbar.css({'left':goleft})
				rowbar.stop().animate({'left':curleft-liwidth+Math.abs(curleft-liwidth)%liwidth},300);

			})/*next-btn*/
	}

})(jQuery);